<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/plugins/select2/select2.css" />
    <link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
    <script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" onclick="delTemplates()" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input id="templateName" >
									<button  onclick="listTypeTemplates()" class="btn btn-default">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="templateBody">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        

                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input id="typeTemplate" class="form-control" placeholder="商品类型">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td id="brandBody">
		      			
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td id="specBody">
	
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" onclick="addBody()" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="addBody">

							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="saveTemplate()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  </div>
</body>
</html>
<script>
	$(function () {
		// 模板表集合
		listTypeTemplates()
		//品牌查询集合
		listBrands()
		//规格表集合
		listSpecifications()
	})

	function saveTemplate() {
		//模板名字
		var templateName=$("#typeTemplate").val();
		//扩展属性
		var customTrs = $("#addBody").find('tr')
		var arr = []
		$(customTrs).each(function (i,e) {
			var customTds = $(e).find('td')
			var customText = $(customTds[1]).find('input').val();
			var custom = {text:customText}
			arr.push(custom)
		})
		console.log(arr)
		var customList = JSON.stringify(arr);

		//规格属性
		var brandList = $("#brandSelect").select2('data')//是一个对象
		var brandArr = []
		$(brandList).each(function (i,e) {
			var brand={id:e.id,text:e.text}
			brandArr.push(brand)
		})
		console.log(brandArr)
		var brands = JSON.stringify(brandArr)

		//品牌属性
		var specList = $("#specSelect").select2('data')
		var specArr = []
		$(specList).each(function (i,e) {
			var spec = {id:e.id,text:e.text}
			specArr.push(spec)
		})
		var specs = JSON.stringify(specArr)

		$.ajax({
			url:"/typeTemplateController/addTemplate",
			data:{name:templateName,customAttributeItems:customList,specIds:specs,brandIds:brands},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				if (result.code==10000){
					alert(result.message)
				}else {
					alert(result.message)
				}
			},
			erroe:function (result) {
				alert("系统错误")
			}
		})
	}

	function listBrands() {
		$.ajax({
			url:"/brandController/listBrands",
			data:"",
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code==10000){
					var brands = result.data;
					var html='<select id="brandSelect" style="width: 100%" multiple>';
					for (var i=0;i<brands.length;i++){
						html+='<option value="'+brands[i].id+'">'+brands[i].name+'</option>'
					}
					html+='</select>'
					$("#brandBody").html(html);
					$("#brandSelect").select2()
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function listBrands2() {
		$.ajax({
			url:"/brandController/listBrands",
			data:"",
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code==10000){
					var brands = result.data;
					var html='<select id="brandSelect2" style="width: 100%" multiple>';
					for (var i=0;i<brands.length;i++){
						html+='<option value="'+brands[i].id+'">'+brands[i].name+'</option>'
					}
					html+='</select>'
					$("#brandBody2").html(html);
					$("#brandSelect2").select2()
				}else {
					alert(result.message)
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}

	function listSpecifications() {
		$.ajax({
			url:"/specificationController/listSpecifications",
			data: {specName:""},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code==10000){
					var specifications = result.data;
					var html = ''
					html+='<select id="specSelect" style="width: 100%" multiple>'
					for (var i=0;i<specifications.length;i++){
						html+='<option value="'+specifications[i].id+'">'+specifications[i].specName+'</option>'
					}
					html+='</select>'
					$("#specBody").html(html);
					$("#specSelect").select2();
				}else {
					result.message
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function listSpecifications2() {
		$.ajax({
			url:"/specificationController/listSpecifications",
			data: {specName:""},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				if (result.code==10000){
					var specifications = result.data;
					var html = ''
					html+='<select id="specSelect2" style="width: 100%" multiple>'
					for (var i=0;i<specifications.length;i++){
						html+='<option value="'+specifications[i].id+'">'+specifications[i].specName+'</option>'
					}
					html+='</select>'
					$("#specBody2").html(html);
					$("#specSelect2").select2();
				}else {
					result.message
				}
			},
			error:function (result) {
				alert("系统错误")
			}
		})
	}
	function listTypeTemplates() {
		$.ajax({
			url:"/typeTemplateController/listTypeTemplates",
			data: {name:$("#templateName").val()},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result) {
				var templates = result.data;
				var html='';
				if (result.code==10000){
					for (var i= 0;i<templates.length;i++){
						var specJson = JSON.parse(templates[i].specIds)
						var brandJson = JSON.parse(templates[i].brandIds)
						var customAttributeItemsJson = JSON.parse(templates[i].customAttributeItems)
						var brandTexts = ""
						var specTexts = ""
						var customAttributeItemsTexts = ""

						$(brandJson).each(function (i,e) {
							brandTexts += ","+e.text
						})

						$(specJson).each(function (i,e) {
							specTexts += ","+e.text
						})

						$(customAttributeItemsJson).each(function (i,e) {
							customAttributeItemsTexts += ","+e.text
						})
						brandTexts = brandTexts.substring(1,brandTexts.length)
						specTexts = specTexts.substring(1,specTexts.length)
						customAttributeItemsTexts = customAttributeItemsTexts.substring(1,customAttributeItemsTexts.length)
						html+='<tr>'
						html+='<td><input name="box" value="'+templates[i].id+'"  type="checkbox"></td>'
						html+='<td>'+templates[i].id+'</td>'
						html+='<td>'+templates[i].name+'</td>'
						html+='<td>'+brandTexts+'</td>'
						html+='<td>'+specTexts+'</td>'
						html+='<td>'+customAttributeItemsTexts+'</td>'
						html+='<td class="text-center">'
						html+='<button type="button" onclick="toUpdate('+templates[i].id+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" >修改</button>'
						html+='</td>'
						html+='</tr>'
					}
					$("#templateBody").html(html)

				}else {
					result.message
				}
			},
			erroe:function (result) {
				alert("系统错误")
			}
		})
	}
	function toUpdate(id) {
			$.ajax({
				url:"/typeTemplateController/selectTemplateById",
				data:{id:id},
				dataType:"json",
				type:"post",
				async:false,
				success:function (result) {
					var templateList = result.data;
					var html = ''

					var customAttributeItemsJson = JSON.parse(templateList.customAttributeItems)

					html+='<div class="modal-dialog" >'
					html+='<div class="modal-content">'
					html+='<div class="modal-header">'
					html+='<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'
					html+='<h3 id="myModalLabel">商品类型模板编辑</h3>'
					html+='</div>'
					html+='<div class="modal-body">'
					html+='<table class="table table-bordered table-striped" width="800px">'
					html+='<tr>'
					html+='<td>商品类型</td>'
					html+='<td><input value="'+templateList.name+'" id="typeTemplate2" class="form-control" placeholder="商品类型">  </td>'
					html+='</tr>'
					html+='<tr>'
					html+='<td>关联品牌</td>'
					html+='<td id="brandBody2">'
					html+='</td>'
					html+='</tr>'
					html+='<tr>'
					html+='<td>关联规格</td>'
					html+='<td id="specBody2">'
					html+='</td>'
					html+='</tr>'
					html+='<tr>'
					html+='<td>扩展属性</td>'
					html+='<td>'
					html+='<div class="btn-group">'
					html+='<button type="button" class="btn btn-default" onclick="addBody2()" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>'
					html+='</div>'
					html+='<table class="table table-bordered table-striped"  width="800px">'
					html+='<thead>'
					html+='<tr>'
					html+='<td><input type="checkbox" class="icheckbox_square-blue"></td>'
					html+='<td>属性名称</td>'
					html+='<td>操作</td>'
					html+='</tr>'
					html+='</thead>'
					html+='<tbody id="addBody2">'
					$(customAttributeItemsJson).each(function (i,e) {
						html+='<tr>'
						html+='<td><input type="checkbox" class="icheckbox_square-blue" ></td>'
						html+='<td><input value="'+e.text+'" class="form-control" placeholder="属性名称" ></td>'
						html+='<td><button type="button" onclick="delBody(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>'
						html+='</tr>'
					})
					html+='</tbody>'
					html+='</table>'
					html+='</td>'
					html+='</tr>'
					html+='</table>'
					html+='</div>'
					html+='<div class="modal-footer">'
					html+='<button class="btn btn-success" onclick="updateTemplate('+templateList.id+')" data-dismiss="modal" aria-hidden="true">保存</button>'
					html+='<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>'
					html+='</div>'
					html+='</div>'
					html+='</div>'
					$("#updateModal").html(html)

					listBrands2()
					listSpecifications2()

					var brandJson = JSON.parse(templateList.brandIds)//转变为一个对象
					var brandArr =[]
					$(brandJson).each(function (i,e) {
						brandArr.push(e.id)
					})
					console.log(brandArr)
					$("#brandSelect2").val(brandArr).trigger("change")

					var specJson = JSON.parse(templateList.specIds)//转变为一个对象
					var specArr = []
					$(specJson).each(function (i,e) {
						specArr.push(e.id)//将id保存到数组里
					})
					console.log(specArr)
					$("#specSelect2").val(specArr).trigger("change")



				},
				erroe:function (result) {
					alert("系统错误")
				}
			})
	}
	function updateTemplate(id) {
		//模板名字
		var templateName=$("#typeTemplate2").val();
		//扩展属性
		var customTrs = $("#addBody2").find('tr')
		var arr = []
		$(customTrs).each(function (i,e) {
			var customTds = $(e).find('td')
			var customText = $(customTds[1]).find('input').val();
			var custom = {text:customText}
			arr.push(custom)
		})
		console.log(arr)
		var customList = JSON.stringify(arr);

		//规格属性
		var brandList = $("#brandSelect2").select2('data')//是一个对象
		var brandArr = []
		$(brandList).each(function (i,e) {
			var brand={id:e.id,text:e.text}
			brandArr.push(brand)
		})
		console.log(brandArr)
		var brands = JSON.stringify(brandArr)

		//品牌属性
		var specList = $("#specSelect2").select2('data')
		var specArr = []
		$(specList).each(function (i,e) {
			var spec = {id:e.id,text:e.text}
			specArr.push(spec)
		})
		var specs = JSON.stringify(specArr)

		$.ajax({
			url:"/typeTemplateController/updateTemplate",
			data:{name:templateName,customAttributeItems:customList,specIds:specs,brandIds:brands,id:id},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result) {
				if (result.code==10000){
					window.location.reload();
				}else {
					alert(result.message)
				}
			},
			erroe:function (result) {
				alert("系统错误")
			}
		})
	}
	function addBody() {
		var html = ""
		html+='<tr>'
		html+='<td><input type="checkbox" class="icheckbox_square-blue" ></td>'
		html+='<td><input class="form-control" placeholder="属性名称" ></td>'
		html+='<td><button type="button" onclick="delBody(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>'
		html+='</tr>'
		$("#addBody").append(html)
	}
	function addBody2() {
		var html = ""
		html+='<tr>'
		html+='<td><input type="checkbox" class="icheckbox_square-blue" ></td>'
		html+='<td><input class="form-control" placeholder="属性名称" ></td>'
		html+='<td><button type="button" onclick="delBody(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>'
		html+='</tr>'
		$("#addBody2").append(html)
	}
	function delBody(bu) {
			if(confirm("确定要删除码?")){
				var tr = bu.parentNode.parentNode;
				tr.parentNode.removeChild(tr);
			}
	}
	function delTemplates() {
		alert("要删除吗");
		var boxs = document.getElementsByName("box");
		var ids = "";
		for (var i = 0;i<boxs.length;i++){
			if (boxs[i].checked){
				ids+=boxs[i].value+","
			}
		}
		ids = ids.substring(0,ids.length-1);
		$.ajax({
			url:"/typeTemplateController/delTemplates",
			data:{ids:ids},
			dataType:"json",
			type:"post",
			async:false,
			success:function (result){
				if (result.code==10000){
					alert("删除成功");
					window.location.reload();
				}else {
					alert("删除失败")
				}
			},
			error:function (){
				alert("系统错误");
			}
		})
	}
</script>